package com.openharmony.filepicker.adapter;

import ohos.aafwk.ability.Ability;
import ohos.agp.components.BaseItemProvider;
import ohos.agp.components.Component;
import ohos.agp.components.ListContainer;

public class RecyclerViewListener implements ListContainer.ItemClickedListener,ListContainer.ItemLongClickedListener{

    private Ability ability;
    private ListContainer recyclerView;
    private OnItemClickListener itemClickListener;

    @Override
    public void onItemClicked(ListContainer listContainer, Component component, int position, long l) {
        if (itemClickListener != null) {
            itemClickListener.onItemClick(listContainer.getItemProvider(),
                    component,
                    position);
        }
    }

    @Override
    public boolean onItemLongClicked(ListContainer listContainer, Component component, int position, long l) {
        if (itemClickListener != null) {
            itemClickListener.onItemLongClick(listContainer.getItemProvider(),
                    component,
                    position);
        }
        return true;
    }

    public interface OnItemClickListener {
        void onItemClick(
                BaseItemProvider adapter,
                Component view,
                int position
        );

        void onItemLongClick(
                BaseItemProvider adapter,
                Component view,
                int position
        );

        void onItemChildClick(
                BaseItemProvider adapter,
                Component view,
                int position
        );
    }

    public RecyclerViewListener(Ability ability,
                                ListContainer recyclerView,
                                OnItemClickListener itemClickListener) {

        this.ability = ability;
        this.recyclerView = recyclerView;
        this.itemClickListener = itemClickListener;

        recyclerView.setItemClickedListener(this::onItemClicked);
        recyclerView.setItemLongClickedListener(this::onItemLongClicked);
        BaseItemProvider itemProvider = recyclerView.getItemProvider();
        if (itemProvider instanceof FileListAdapter){
            ((FileListAdapter) itemProvider).setItemChildrenClickListener(new FileListAdapter.ItemChildrenClickListener() {
                @Override
                public void onItemChildrenClick(Component component, int position) {
                    itemClickListener.onItemChildClick(itemProvider,component,position);
                }
            });
        }
    }
}
